const { Controller } = require('egg');

class ApprovalController extends Controller {

  /**
   * @api {POST} /product/approval/insert 新增
   * @apiName insert
   * @apiGroup approval
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiBody  { String } barcode_id 条码id
   * @apiBody  { Number } price 价格
   * @apiBody  { String } product_id 产品id
   * @apiBody  { String } type 类型
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async insert() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.request.body;

      const { barcode_id, number, type, product_id, status, title } = await app.rule.approval.insert(value);

      const data = await ctx.service.approval.insert(barcode_id, number, type, product_id, status, title);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {GET} /product/approval/refuse 拒绝审批
   * @apiName refuse
   * @apiGroup approval
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } approval_id 审批id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async refuse() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const value = ctx.query;
      const { approval_id } = await app.rule.approval.refuse(value);
      const data = await ctx.service.approval.refuse(approval_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {GET} /product/approval/pass 通过审批
   * @apiName pass
   * @apiGroup approval
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } approval_id 审批id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async pass() {
    const ctx = this.ctx;
    const app = this.app;
    try {
      const value = ctx.query;

      const { approval_id } = await app.rule.approval.pass(value);

      const data = await ctx.service.approval.pass(approval_id);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api {GET} /product/approval/list 获取审批列表
   * @apiName list
   * @apiGroup approval
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } name 名称
   * @apiQuery  { String } barcode 条码
   * @apiQuery  { String } type 类型
   * @apiQuery  { String } order 排序字段
   * @apiQuery  { String } method 排序方式
   * @apiQuery  { Number } pageIndex 页码
   * @apiQuery  { Number } pageSize 页面容量
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async list() {
    const ctx = this.ctx;
    const app = this.app;
    try {

      const value = ctx.query;

      const { name, barcode, status, type, order, method, pageIndex, pageSize } = await app.rule.approval.list(value);

      const data = await ctx.service.approval.list(name, barcode, status, type, order, method, pageIndex, pageSize);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api {GET} /product/approval/info 获取审批信息
   * @apiName info
   * @apiGroup approval
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } id 审批id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async info() {
    const ctx = this.ctx;
    try {

      const value = ctx.query;

      const { id } = await ctx.app.rule.approval.info(value);

      const data = await ctx.service.approval.info(id);

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = ApprovalController;
